﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private void ShowMessage(string msg)
        {
            this.Panel_Msg.Visible = true;
            this.Message.Text = "·" + msg;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            this.Panel_Msg.Visible = false;
            if (!this.IsPostBack)
            {
                this.Button_Save.Attributes.Add("onclick", "calc();");
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }
                if (id > 0)
                {
                    this.TianBao.DataBind();
                    this.BillID.Text = id.ToString();
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = this.BillID.Text;
                    this.SqlDataSource1.SelectCommand =
@"select top 1 @年度=年度,@月份=月份,@填报单位=rtrim(填报单位),@填报日期=填报日期 from 辅料采购计划0 where ID=@ID or MID=@ID;
select cast(t1.辅料ID as varchar)
+'|=*'+isnull(t2.品名,'')
+'|=*'+isnull(t2.规格,'')
+'|=*'+isnull(t2.单位,'')
+'|=*'+cast(t1.填报数量 as varchar)
+'|=*'+cast(t1.单价 as varchar)
+'|=*'+isnull(t1.用途,'')
+'|=*'+isnull(t1.备注,'') as val
,'' as txt
from 辅料采购计划0 t1
left outer join 材料 t2 on t1.辅料ID=t2.ID
where t1.状态=0 and (t1.id=@id or t1.mid=@id)";
                    DateTime d = DateTime.Today;
                    this.NianDu.Text = d.Year.ToString();
                    this.YueFen.Text = d.Month.ToString();
                }
                else
                {
                    DateTime d = DateTime.Today;
                    this.NianDu.Text = d.Year.ToString();
                    this.YueFen.Text = d.Month.ToString();
                    this.Date1.Text = d.ToLongDateString();
                }
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string[] cell_id = this.Cell_ID.Value.Split(new string[] { "|" }, StringSplitOptions.None);

            string[] cell_pm = this.Cell_PM.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            string[] cell_xh = this.Cell_XH.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            string[] cell_dw = this.Cell_DW.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);

            string[] cell_sl = this.Cell_SL.Value.Split(new string[] { "|" }, StringSplitOptions.None);
            string[] cell_dj = this.Cell_DJ.Value.Split(new string[] { "|" }, StringSplitOptions.None);

            string[] cell_ty = this.Cell_YT.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            string[] cell_bz = this.Cell_BZ.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);

            //this.RowList.Items.Clear();
            //for (int i = 1; i < cell_id.Length; i++)
            //{
            //    ListItem li = new ListItem();
            //    li.Text = cell_id[i] + "|=*" + cell_pm[i] + "|=*" + cell_xh[i] + "|=*" + cell_dw[i]
            //        + "|=*" + cell_sl[i] + "|=*" + cell_dj[i] + "|=*" + cell_ty[i] + "|=*" + cell_bz[i];
            //    li.Value = "";
            //    this.RowList.Items.Add(li);
            //}
            DateTime d;
            if (!DateTime.TryParse(this.Date1.Text, out d))
            {
                this.ShowMessage("“填报日期”录入有误");
                return;
            }
            if (this.BillID.Text != "0")
            {
                this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.BillID.Text;
                this.SqlDataSource1.Delete();
            }
            this.SqlDataSource1.InsertParameters["年度"].DefaultValue = this.NianDu.Text;
            this.SqlDataSource1.InsertParameters["月份"].DefaultValue = this.YueFen.Text;

            this.SqlDataSource1.InsertParameters["填报单位"].DefaultValue = this.TianBao.Text;
            this.SqlDataSource1.InsertParameters["填报日期"].DefaultValue = d.ToShortDateString();

            for (int i = 1; i < cell_id.Length; i++)
            {
                this.SqlDataSource1.InsertParameters["ID"].DefaultValue = this.BillID.Text;
                this.SqlDataSource1.InsertParameters["辅料ID"].DefaultValue = cell_id[i];
                this.SqlDataSource1.InsertParameters["填报数量"].DefaultValue = cell_sl[i];
                this.SqlDataSource1.InsertParameters["单价"].DefaultValue = cell_dj[i];
                this.SqlDataSource1.InsertParameters["金额"].DefaultValue = (double.Parse(cell_sl[i]) * double.Parse(cell_dj[i])).ToString();
                this.SqlDataSource1.InsertParameters["用途"].DefaultValue = cell_ty[i];
                this.SqlDataSource1.InsertParameters["备注"].DefaultValue = cell_bz[i];
                this.SqlDataSource1.Insert();
            }
            this.ShowMessage("保存成功");

        }
        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            this.BillID.Text = ((int)e.Command.Parameters[0].Value).ToString();
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            this.NianDu.Text = ((short)e.Command.Parameters[1].Value).ToString();
            this.YueFen.Text = ((byte)e.Command.Parameters[2].Value).ToString();
            this.TianBao.Text = (string)e.Command.Parameters[3].Value;
            this.Date1.Text = ((DateTime)e.Command.Parameters[4].Value).ToLongDateString();

        }
    }
}
